package com.wan37.logic.handler.server;

import com.wan37.logic.component.Worker;
import com.wan37.logic.db.AsynDBManager;
import com.wan37.logic.domain.entity.RoleDO;
import com.wan37.logic.service.RoleService;
import com.wan37.protobuf.constant.AllRoleLabel;
import com.wan37.protobuf.constant.CommonBase;
import com.wan37.logic.domain.vo.RoleVO;
import com.wan37.logic.global.reference.SpringContextHolder;
import com.wan37.logic.handler.WorkerHandler;
import com.wan37.logic.service.cache.HpServiceCache;
import com.wan37.logic.service.cache.RoleServiceCache;
import com.wan37.logic.service.cache.SceneServiceCache;
import com.wan37.logic.service.cache.UserServiceCache;
import com.wan37.logic.utils.RouteUtils;
import com.wan37.protobuf.constant.Common;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : luoyong
 * @date : 2020-06-09 14:56
 **/
public class AutoAddHpHandler implements WorkerHandler {

    private static final Logger logger = LoggerFactory.getLogger("AutoAddHpHandler.class");

    private String rollId;
    private RoleServiceCache roleServiceCache;
    private HpServiceCache hpServiceCache;
    private RouteUtils routeUtils;
    private SceneServiceCache sceneServiceCache;
    private UserServiceCache userServiceCache;
    private AsynDBManager asynDBManager;
    private RoleService roleService;

    public AutoAddHpHandler(String rollId) {
        this.rollId = rollId;
    }


    @Override
    public void execute(Worker worker) throws Exception {
        this.roleServiceCache = SpringContextHolder.getBean("roleServiceCache");
        this.hpServiceCache = SpringContextHolder.getBean("hpServiceCache");
        this.routeUtils = SpringContextHolder.getBean("routeUtils");
        this.sceneServiceCache = SpringContextHolder.getBean("sceneServiceCache");
        this.userServiceCache = SpringContextHolder.getBean("userServiceCache");
        this.asynDBManager = SpringContextHolder.getBean("asynDBManager");
        this.roleService = SpringContextHolder.getBean("roleService");
        RoleVO roleVO = roleServiceCache.getRoleServiceMap().get(rollId).getRoleVO();
        if(roleVO.getStatus() == AllRoleLabel.STATUS_DIE){
            return;
        }
        long hp = roleVO.getNumberMap().get(CommonBase.HP) + hpServiceCache.AUTO_ADD_HP_NUM * roleVO.getLevel();
        if (hp > 10000) {
            roleVO.getNumberMap().put(CommonBase.HP, (long) 10000);
            logger.info("----------当前角色HP: 10000");
        } else {
            roleVO.getNumberMap().put(CommonBase.HP, hp);
            logger.info("----------当前角色HP: " + hp);
        }

        if (userServiceCache.getUserServiceMap().containsKey(roleVO.getUserId())) {
            List<RoleVO> roleVOList = new ArrayList<>();
            roleVOList.add(roleVO);
            routeUtils.sendAoiRoleBroadcastResponse(Common.SERVER_AUTO_HP_REVERT,
                    sceneServiceCache.getSceneServiceMap().get(userServiceCache.getUserServiceMap()
                            .get(roleVO.getUserId()).getSceneId()).getUserIdSet(), roleVOList);
            RoleDO roleDO = new RoleDO(roleVO);
            this.asynDBManager.distributeWork(roleVO.getUserId(), () -> roleService.update(roleDO));
        }
    }
}
